package com.example.rpcdemo.rpc.client;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class RpcFuture {
    private final CountDownLatch latch = new CountDownLatch(1);
    private Object result;
    private Throwable error;
    
    public Object get(long timeout, TimeUnit unit) throws Exception {
        if (!latch.await(timeout, unit)) {
            throw new TimeoutException("RPC request timeout");
        }
        
        if (error != null) {
            throw new Exception("RPC request failed", error);
        }
        
        return result;
    }
    
    public void setSuccess(Object result) {
        this.result = result;
        latch.countDown();
    }
    
    public void setFailure(Throwable error) {
        this.error = error;
        latch.countDown();
    }
}